package com.gdwz.ps.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xzsoft.gdwz.services.sendLimitVendorService;

import xz.facade.user.model.Result;

/**
 * <p>
 * Title:
 * </p>
 * <p>
 * Description:
 * </p>
 * <p>
 * Company:兴竹信息
 * </p>
 * 
 * @author:杨 光
 * @date:2016-12-5 下午2:49:32
 */

@Controller
@RequestMapping("/limitQuotePrice")
public class LimitQuotePriceController {

	Logger logger = Logger.getLogger(LimitQuotePriceController.class);

	@Autowired
	private sendLimitVendorService sendLimitVendorService;

	/**
	 * 回传限时报价供应商
	 * 
	 * @param request
	 * @param response
	 * @throws IOException
	 *             void
	 */
	@RequestMapping("/vendor")
	public void reciveVendor(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		String array = request.getParameter("inqVendor");
		logger.info("array");
		Result result = new Result();
		JSONArray resultArray = null;
		if (array == null || "".equals(array)) {
			result.setResult("");
			result.setResultCode("C0013");
			result.setResultMessage("inqVendor参数不能为空！");
			result.setSuccess(false);
			JSONObject jb = JSONObject.fromObject(result);
			response.getWriter().println(jb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			return;
		}
		try {
			resultArray = JSONArray.fromObject(array);
		} catch (Exception e) {
			e.printStackTrace();
			result.setResult("");
			result.setResultCode("C0011");
			result.setResultMessage("JSONArray格式不正确！");
			result.setSuccess(false);
			JSONObject jb = JSONObject.fromObject(result);
			response.getWriter().println(jb.toString());
			response.getWriter().flush();
			response.getWriter().close();
			e.printStackTrace();
			return;
		}
		// 保存正确限时报价供应商
		try {
			sendLimitVendorService.saveInqVendor(resultArray);
		} catch (Exception e) {
			e.printStackTrace();
			result.setResult("");
			result.setResultCode("C0012");
			result.setResultMessage(e.getMessage());
			result.setSuccess(false);
			JSONObject jb = JSONObject.fromObject(result);
			response.getWriter().println(jb.toString());
			response.getWriter().flush();
			response.getWriter().close();
		}
		result.setResult("");
		result.setResultCode("");
		result.setResultMessage("");
		result.setSuccess(true);
		JSONObject jb = JSONObject.fromObject(result);
		response.getWriter().println(jb.toString());
		response.getWriter().flush();
		response.getWriter().close();
	}

}